/**
 * File  : EventQueue.h
 * Time  : 2014-12-19 16:16
 * Author: KashingLee
 * Ref   : Null
 */
#ifndef PLCC_EVENT_QUEUE_EVENT_QUEUE_
#define PLCC_EVENT_QUEUE_EVENT_QUEUE_
#include "Queue.h"
#include "../thread/Event.h"
#include "../thread/Mutex.h"
#include<iostream>
using namespace std;
template <class Element>
class EventQueue {
 public:
  EventQueue() {};
  ~EventQueue(){};

 public:
  void  AddEvent(const Element &code);
  Type<Element>* GetEvent();
  bool EventQueue_Isempty();

 private:
  Queue<Element> q_;
  Mutex mutex_;
  Event event_;
}; 

template <class Element>
void EventQueue<Element>::AddEvent(const Element &code) {
  AutoMutex<Mutex>tmp(mutex_);
  bool need_signal = q_.IsEmpty();
  q_.EnQueue(code);
  if (need_signal) {
    event_.Signal();
  }
}

template <class Element>
Type<Element>* EventQueue<Element>::GetEvent() {
  AutoMutex<Mutex>tmp(mutex_);
  while (q_.IsEmpty()) {
    //cout << "empty" <<endl;
   // event_.Wait(mutex_);
			return NULL;
  }
  return q_.DeQueue();
}

template <class Element>
bool EventQueue<Element>::EventQueue_Isempty()
{

  AutoMutex<Mutex>tmp(mutex_);

  if(q_.IsEmpty())
     return true;
  else
     return false;

}
#endif // PLCC_EVENT_QUEUE_EVENT_QUEUE_
